#define  _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <set>
#include <string>
using namespace std;

struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};

class Solution {
public:
	ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2) {
		typedef ListNode Node;
		int size1 = 0, size2 = 0, len = 0;
		Node* cur1 = pHead1, * cur2 = pHead2;
		while (cur1)
		{
			size1++;
			cur1 = cur1->next;
		}
		while (cur2)
		{
			size2++;
			cur2 = cur2->next;
		}
		len = abs(size1 - size2);

		Node* shortlt = pHead1;
		Node* longlt = pHead2;
		if (size1 > size2)
		{
			shortlt = pHead2;
			longlt = pHead1;
		}

		while (len--)
		{
			longlt = longlt->next;
		}

		while (longlt && shortlt)
		{
			if (longlt == shortlt)
			{
				return shortlt;
			}
			longlt = longlt->next;
			shortlt = shortlt->next;
		}

		return nullptr;
	}
};

int main()
{
	return 0;
}

//int main()
//{
//    set<char> s;
//    string s1, s2, ret;
//    getline(cin, s1);
//    getline(cin, s2);
//    for (auto& e : s2)
//    {
//        s.insert(e);
//    }
//
//    for (auto& e : s1)
//    {
//        if (s.count(e) == 0)
//        {
//            ret.push_back(e);
//        }
//    }
//
//    cout << ret;
//    return 0;
//}